/**
 * @Author:Kindear
 * @Extend:校园小程序 & 练手Lab
 * @Todo:获取的今日课表进行排序
 */
const app = getApp();
import config from "../../config";
import dayjs from "dayjs";
const timeutil = require('timeutil');
Page({
    data: {
        classTime: config.classTime,
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        displayArea: app.globalData.displayArea,
        ImgUrl: app.globalData.ImgUrl,
        colorList: config.colorList, // 色彩列表
        timetableData: null
    },
    /**获取用户基础信息 
     * 进行上传绑定
     */
    bindUser(e) {
        wx.getUserProfile({
            desc: '获取头像和昵称',
            success: res => {
                let userInfo = res.userInfo
                wx.showLoading({
                    title: '登录',
                    mask: true
                })
                wx.cloud.callFunction({
                    name: 'cloud-user-api',
                    data: {
                        action: 'userBind',
                        userInfo: userInfo
                    },
                    complete: res => {
                        console.log(res)
                        this.initTable();
                        wx.hideLoading();
                    }
                })
            }
        })
    },
    /** Page - 生命周期 -- 初次装入 */
    onLoad(options) {
        var that = this;
        /**如果存在copyId */
        if (options.copyId == undefined) {
            this.initTable();
        } else {
            console.log('复制该用户课表')
            wx.showModal({
              title:'提醒',
              content:'是否要复制该用户课表',
              success:r=>{
                  if(r.confirm){
                      wx.showLoading({
                        title: '复制课表中',
                      })
                      wx.cloud.callFunction({
                          name:'edu-timetable-api',
                          data:{
                              action:'copyTable',
                              copyId:options.copyId
                          },
                          success:res=>{
                              if(res.result.errCode == -1){
                                  that.setData({
                                      errMsg:res.result.errMsg
                                  })
                              }
                              this.initTable();
                           
                          },
                          complete:res=>{
                              console.log(res)
                              wx.hideLoading();
                          }
                      })
                  }
              }
            })
        }
    },
    /**请求获取基础课表信息 */
    /**初始化课表 */
    initTable() {
        wx.showLoading({
            title: '获取今日课表',
        })
        var that = this;
        /**获取当前星期几 */
        let days = dayjs().day()
        // days = 3
        let hour = dayjs().hour();
        let minute = dayjs().minute();
        let timeStr = timeutil.formatNumber(hour) + ":" + timeutil.formatNumber(minute)
        console.log(timeStr)
        // console.log(days)
        wx.cloud.callFunction({
            name: 'edu-timetable-api',
            data: {
                action: 'initTable'
            },
            success: res => {
                /**获取成功 */
                if (res.result.errCode == -1) {
                    /**提示错误信息 */
                    wx.showToast({
                        title: res.result.errMsg,
                        icon: 'none'
                    })
                    this.setData({
                        todayCourse: []
                    })
                } else {
                    let todayCourse = []
                    // console.log(days)
                    let timetableData = res.result.timetableData;
                    timetableData.forEach(function (ele, index) {
                        if (days == ele.days) {
                            todayCourse.push(ele)
                        }
                    })
                    /**对返回结果进行排序 */
                    todayCourse = todayCourse.sort(function (a, b) {
                        return a.start_time - b.start_time
                    })
                    console.log(todayCourse)
                    that.setData({
                        userInfo: res.result.userInfo,
                        todayCourse,
                        timetableData
                    })

                }
            },
            complete: res => {
                wx.hideLoading();
                console.log(res)
            }
        })
    },
    onShareAppMessage(){
        // let userInfo = wx.getStorageSync('userInfo')
        // // console.log(userInfo)
        // let realName = userInfo.realName;
        var that = this;
        let userInfo = that.data.userInfo
        return {
            title: `${userInfo.nickName}分享的课表`,
            path: 'pages/index/index?copyId='+userInfo.openid
        }
    }
});